<?php

include_once 'dbtable.php';

function get_comment_by_posts($id) {
//    $sobghi = count(DbTable::getData("select * from comment where post_id = '{$id}' and status = 1"));
//    if($sobghi > 3){
//        $dau = $sobghi - 3;
//    }else{
//        $dau = 0;
//    }
    //$sql = "select user.*,comment.* from user,comment where user.id = comment.user_id and comment.status = 1 and comment.post_id = '{$id}' order by comment.id asc limit {$dau},{$sobghi}";
    $sql = "select user.*,comment.* from user,comment where user.id = comment.user_id and comment.status = 1 and comment.post_id = '{$id}' order by comment.id asc";
    return DbTable::getData($sql);
}
function get_comment_show_all($id) {
    $sobghi = count(DbTable::getData("select * from comment where post_id = '{$id}' and status = 1"));
    if($sobghi > 3){
        $dau = $sobghi - 3;
    }else{
        $dau = 0;
    }
    $sql = "select user.*,comment.* from user,comment where user.id = comment.user_id and comment.status = 1 and comment.post_id = '{$id}' order by comment.id asc limit 0,{$dau}";
    return DbTable::getData($sql);
}
function get_comment_chi_tiet($id) {
//    $sobghi = count(DbTable::getData("select * from comment where post_id = '{$id}' and status = 1"));
//    if($sobghi > 3){
//        $dau = $sobghi - 3;
//    }else{
//        $dau = 0;
//    }
    $sql = "select user.*,comment.* from user,comment where user.id = comment.user_id and comment.status = 1 and comment.post_id = '{$id}' order by comment.id asc";
    return DbTable::getData($sql);
}
function them_moi_comment($array) {
    DbTable::insert("comment", $array);
    
    $id = mysql_insert_id();
    $sql = "select user.*,comment.* from user,comment where user.id = comment.user_id and comment.status = 1 and comment.id = '{$id}' limit 0,3";
    
    $item = DbTable::First($sql);
    
    echo "<li class='cm{$item[18]}'>";
        if($item[0] == $_COOKIE["id"]){ 
            echo "<span id='{$item[18]}' title='Xóa comment này' style='padding:1px;background: #fff;border:1px solid #ccc;cursor:pointer;' class='delete_comment'>x</span>";
        }
        echo '<div style="float:left">';
            echo "<img width='28px' height='28px' class='img-border' src='data/avatar/{$item[15]}'/>";
        echo "</div>";
        echo '<div style="float: left;padding-left:5px;width:380px;word-wrap:break-word">';
            echo "<b style='color:#286fc7'>{$item[4]} </b>";            
            echo $item[22];
            echo "<br/><br/>";
            $date = date("F j, Y, g:i a",  strtotime("{$item[21]}"));
            echo "<span style='color:#c76728'>Đăng lúc {$date} </span>";
        echo "</div>";
        echo '<div style="clear: both"></div>';
    echo "</li>";
    
}
function xoa_comment($id) {
    if(DbTable::delete("comment", $id)){
        return true;
    }
    return false;
}
function lay_thanh_vien_comment() {
    $sql = "select comment.*,user.* from 
            comment,user where comment.user_id = user.id
             order by comment.id desc limit 0,5            
        ";
    return DbTable::getData($sql);
}
function get_baiviet_post_id($id) {
    $sql = "select post.*,user.* from post,user where post.user_id = user.id and post.id = '{$id}'";
    return DbTable::First($sql);
}

function get_comment_by_time() {
    
    foreach(lay_thanh_vien_comment() as $tv){
    echo "<li>";
        echo "<div>";
            echo '<div style="float: left;">';
                echo "<img class='img-border' src='data/avatar/{$tv["avatar"]}' width='40px' height='40px' alt='{$tv["avatar"]}'/>";
            echo "</div>";
            echo '<div style="float:left;width:150px;word-wrap:break-word;padding-left:3px;">';
                echo "<p>";
                    $post = get_baiviet_post_id($tv[1]);
                    echo "<b style='color:#3087ca'>{$tv["username"]}</b> <i><a style='padding-left: 0px;' href='comment.php?pid={$post[0]}'>comment bài viết của</i></a> <b>{$post[7]}</b>";
                echo "</p>";
                echo "<br/>";
                echo "<p>";
                    $date = date("d/m/Y g:i a",  strtotime("{$tv["date_comment"]}"));
                    echo '<img src="Images/icon/comment.gif" all="comment"/>';  echo $date;
                echo "</p>";
            echo "</div>";
        echo "</div>";
        echo '<div style="clear: both"></div>';
    echo "</li>  ";
    }
    
}

function them_vao_log($array) {
    DbTable::insert("log", $array);
    $id = mysql_insert_id();
    
    
    
}
?>
